package com.example.himalaya2.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya2.DetailActivity;
import com.example.himalaya2.R;
import com.example.himalaya2.adapters.RecommendListAdapter;
import com.example.himalaya2.base.BaseFragment;
import com.example.himalaya2.interfaces.IRecommendCallBack;
import com.example.himalaya2.presenters.AlbumDetailPresenter;
import com.example.himalaya2.presenters.RecommendPresenter;
import com.example.himalaya2.utils.Constants;
import com.example.himalaya2.utils.LogUtil;
import com.example.himalaya2.views.UILoader;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendFragment extends BaseFragment implements IRecommendCallBack,UILoader.OnRetryClickListener,RecommendListAdapter.OnRecommendItemClickListener {
    private static final String TAG ="RecommendFragment";

    private View mRootView;
    private RecyclerView mRecommendRv;
    private RecommendListAdapter mRecommendListAdapter;
    private RecommendPresenter mRecommendPresenter;

    private UILoader mUiLoader;

    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, @Nullable ViewGroup container) { // 被fragment的onCreateView调用，返回一个被子布局嵌入后的父布局
        LogUtil.d(TAG,"onSubViewLoaded");
        mUiLoader = new UILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSuccessView(layoutInflater,container);
            }
        };

        // 获取到逻辑对象
        mRecommendPresenter =  RecommendPresenter.getInstance();
        // 先要设置通知接口的注册
        mRecommendPresenter.registerViewCallback(this);  // 该类实现了IRecommendCallBack接口，注册后 ，
                                                        // 对方就持有了对该RecommendFragment对象的引用
        // 获取到推荐列表
        mRecommendPresenter.getRecommendList();
        if (mUiLoader.getParent() instanceof ViewGroup) { // 避免出现多次绑定的情况
            ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);  // 取消绑定
        }
        mUiLoader.setOnRetryClickListener(this);
        // 返回view给界面显示
        return mUiLoader;  // mUiLoader继承了FFrameLayout
    }

    @Override
    public void onAttachFragment(@NonNull @NotNull Fragment childFragment) {
        super.onAttachFragment(childFragment);
        
    }

    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        mRootView = layoutInflater.inflate(R.layout.fragment_recommend,container,false);
        // RecycleView 的使用
        // 1.找对应的控件
        mRecommendRv = mRootView.findViewById(R.id.recommend_list);
        // 2.设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);  // 设置方向
        mRecommendRv.setLayoutManager(linearLayoutManager);
        mRecommendRv.addItemDecoration(new RecyclerView.ItemDecoration() { // 添加间隙
            @Override
            public void getItemOffsets(@NonNull @NotNull Rect outRect, @NonNull @NotNull View view, @NonNull @NotNull RecyclerView parent, @NonNull @NotNull RecyclerView.State state) {
                //super.getItemOffsets(outRect, view, parent, state);
                outRect.top = UIUtil.dip2px(view.getContext(),5);
                outRect.bottom = UIUtil.dip2px(view.getContext(),5);
                outRect.left = UIUtil.dip2px(view.getContext(),5);
                outRect.right = UIUtil.dip2px(view.getContext(),5);
            }
        });
        // 3. 设置适配器
        mRecommendListAdapter = new RecommendListAdapter();
        mRecommendRv.setAdapter(mRecommendListAdapter);
        mRecommendListAdapter.setOnRecommendItemClickListener(this);
        return mRootView;
    }


    @Override
    public void onRecommendListLoaded(List<Album> result) {
        LogUtil.d(TAG,"void onRecommendListLoaded(List<Album> result)");
        // 当我们获取到推荐内容时 这个方法就会被调用
        // 数据回来后更新  ui
        mRecommendListAdapter.setData(result);
        mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }


    @Override
    public void onNetworkError() {
        LogUtil.d(TAG,"onNetworkError()");
        mUiLoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
    }

    @Override
    public void onEmpty() {
        LogUtil.d(TAG,"onEmpty()");
        mUiLoader.updateStatus(UILoader.UIStatus.EMPTY);
    }

    @Override
    public void onLoading() {
        LogUtil.d(TAG,"onLoading()");
        mUiLoader.updateStatus(UILoader.UIStatus.LOADING);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 取消接口的注册，避免内存的泄露
        if (mRecommendPresenter !=null) {
            mRecommendPresenter.unRegisterViewCallback(this);
        }
    }

    @Override
    public void onLoaderMore(List<Album> result) {

    }

    @Override
    public void onRefreshMore(List<Album> result) {

    }

    @Override
    public void onRetryClick() {
        // 网络不佳的时候，用户点击了重试
        //重新获取数据
        if (mRecommendPresenter !=null) {
            mRecommendPresenter.getRecommendList();
        }
    }

    @Override
    public void onItemClick(int position,Album album) {
        AlbumDetailPresenter.getInstance().setTargetAlbum(album);
        // item 被点击了,跳转到详情页
        Intent intent = new Intent(getContext(), DetailActivity.class);
        startActivity(intent);
    }
}
